require 'rubygems'
require 'flickraw'

class FlickrUploader

  attr_reader :api_key, :shared_secret
  
  def initialize(api_key, shared_secret)
    @api_key = api_key
    @shared_secret = shared_secret
    update_flickr_constants
  end
  
  def request_new_token
    frob = flickr.auth.getFrob
    auth_url = FlickRaw.auth_url(:frob => frob, :perms => 'write')
    Help.request_flickr_token(auth_url)    
    STDIN.gets
    response = flickr.auth.getToken :frob => frob
    response.token
  end
  
  def load_token(token)
    flickr.auth.checkToken :auth_token => token
  end
  
  def login
    flickr.test.login
  end

  def upload_pictures(all_pictures)
    number_of_pictures = 0
    all_pictures.each do |path_to_photo| 
      number_of_pictures += upload_picture(path_to_photo)
    end
    number_of_pictures
  end
  
  def upload_picture(path_to_photo)
    Help.unknown_file(path_to_photo) and return 0 unless File.exists? path_to_photo
    flickr.upload_photo path_to_photo, :is_public => 0, :hidden => 2
    return 1
  end
  
  private
    
    def update_flickr_constants
      FlickRaw.api_key = @api_key
      FlickRaw.shared_secret = @shared_secret    
    end

end